// ==UserScript==
// @name        eRep Orders
// @version     7
// @description eRepublik Hungarian Orders
// @author      IM
// @namespace   IM
// @include     http://*.erepublik.com/en
// @include     http://erepublik.com/en
// @include     http://*.erepublik.com/hu
// @include     http://erepublik.com/hu
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require     http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js
// ==/UserScript==

var orderObj = {};
var thisVersion = 7;
var now = new Date().getTime();
var HOUR = 3600000; // óránként ellenőrizni

function showRefresh() {
	if (thisVersion != lastVersion) {
		if (thisVersion > lastVersion) {
			$('#eHUNOrderRefresh').html('<br/><a style="color:red;" href="http://ereptoolkit.googlecode.com/svn/order/erep_orders.v'+lastVersion+'.user.js">Ez a hivatalosnál ('+lastVersion+') frissebb verzió. Innen visszatérhetsz a hivatalosra</a>' );
		} else { 
			$('#eHUNOrderRefresh').html('<br/><a style="color:red;" href="http://ereptoolkit.googlecode.com/svn/order/erep_orders.v'+lastVersion+'.user.js">Telepítsd az új verziót: '+lastVersion +' (Jelenleg:'+thisVersion+')</a>' );
		}
	}
}

function refresh() {
	lastRefresh = GM_getValue('eHunOrderScriptTime',0)*1;
	lastVersion = GM_getValue('eHunOrderScriptVersion',1);
	GM_log('refresh '+now +' vs ' + (lastRefresh+HOUR) +',' + lastRefresh +',' + (now > lastRefresh+HOUR));
	if (now > lastRefresh+HOUR) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'http://ereptoolkit.googlecode.com/svn/order/last?'+now,
            onload:function(responseDetails){
				lastVersion = responseDetails.responseText*1;
				showRefresh();
				GM_setValue('eHunOrderScriptTime',''+now);
				GM_setValue('eHunOrderScriptVersion',''+lastVersion);
			}
		})
	}
	showRefresh();
}

function Main(e) {
		nick = $('div.nameholder a:first').html(); 
		$('#latestnews').before(
			'<div style="background-color:#e9f5fa; border: 1px solid #c6dde6; padding:0px; margin:0px;"\
			><img width="330" height="30" src="http://ereptoolkit.googlecode.com/svn/order/fejlec.png"\
			/><div id="eHUNOrder"></div><div id="eHUNOrderRefresh" ></div\
			><table width="333"><tr height="30" style="background-image:url(\'http://ereptoolkit.googlecode.com/svn/order/hatter.png\'); background-repeat:repeat-x;">\
			<td><a href="http://www.erepublik.com/en/newspaper/hadugyi-kozlony-177586/1" title="Hadügyi közlöny" target="_blank">\
			   <img src="http://ereptoolkit.googlecode.com/svn/order/kozlony.png"/>\
			</a></td>\
			<td><a href="http://widget.mibbit.com?channel=%23erep&nick='+(nick?nick:'Ide a nevedet')+'" title="Erep csatorna a Mibbiten" target="_blank">\
			   <img src="http://ereptoolkit.googlecode.com/svn/order/mibbit.png"/>\
			</a></td>\
			<td><a href="http://widget.mibbit.com?channel=%23hadtap&nick='+(nick?nick:'Ide a nevedet')+'" title="Fegyverosztás csatorna a Mibbiten" target="_blank">\
			   <img src="http://ereptoolkit.googlecode.com/svn/order/osztas.png" />\
			</a></td>\
			<td width="100%"></td>\
			</tr></table></div>');
        GM_xmlhttpRequest({
                method: 'GET',
                url: 'http://erepmarket.com/ehungary/parancs/parancs.json?'+now,
                onload:function(responseDetails){
                        var responseText = responseDetails.responseText;
                        orderObj = eval('('+unescape(responseText)+')');
                        $('#eHUNOrder').html(
                        '<br/><br/><a style="font-size:16px; font-family:Verdana; color:black;"'
                        +' href="http://www.erepublik.com/en/battles/show/'
                        +orderObj.battleid+'"><table><tr><td valign="top"><IMG style="padding-right:5px;" SRC="http://ereptoolkit.googlecode.com/svn/order/fight.jpg"\
                        ></td><td valign="top" style="font-size:16px; font-family:Verdana; color:black;">'+orderObj.text+'   (' + orderObj.region 
                        + ') <br/><font  style="font-size:10px; font-family:Verdana;  padding-top:6px">Vége: '+orderObj.endtime 
                        +'</font></td></tr></table></a>'
                        +'<br/>'
                        +'<p align="right" style=" font-style: italic; padding-top:10px; padding-right:5px;">Kiadva: '+orderObj.timestamp +' by '+ orderObj.editor+' </p>'
                        );
                }       
        });
        refresh();
}

window.addEventListener('load', function(){var checker=setInterval(function(){
        if(typeof ($ = jQuery.noConflict()) != "undefined") {
                clearInterval(checker);
                Main();
        }
},100);}, false);
